﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;
using HTT.Utility;

public partial class Admin_Module_News_Controls_NewsAE2 : System.Web.UI.UserControl
{
    public Guid NewsId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["NewsId"] != null)
        {
            NewsId = new Guid(Request["NewsId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else NewsId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadDLL();
            LoadData();
        }

    }
    private void LoadDLL()
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add("Name ASC");
        ddlCategory.DataSource = new TblCategoryRepository().SelectListForDDL(null, 0);
        ddlCategory.DataTextField = "Name";
        ddlCategory.DataValueField = "Id";
        ddlCategory.DataBind();
    }
    private void LoadData()
    {
        if (!NewsId.Equals(Guid.Empty))
        {
            //IList<TblNews> ls = new TblNewsRepository().SelectListItem(NewsId, null, "", "", "", "", null, null, null, new HashSet<string>());
            TblNews item = TblNewsRepository.CreateInstant().SelectOne(NewsId);
            if (item != null)
            {
                txtName.Text = item.Title;
                txtSummary.Text = item.Summary;
                radEditorContent.Content = item.Description;
                ddlCategory.SelectedValue = item.CategoryId.ToString();
                cbIsVisible.Checked = item.IsVisible;
                cbHot.Checked = item.IsHot;

                hdfimg.Value = item.ImageUrl;
                imgThumb.ImageUrl = item.ImageUrl;
                lblCreatedBy.Text = item.CreatedBy;
                lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
                lblUpdatedBy.Text = item.UpdatedBy;
                lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
            }
        }
    }
    private TblNews GetTblNews()
    {
        TblNews obj = TblNewsRepository.CreateInstant().SelectOne(NewsId);
        if (obj == null)
        {
            obj = new TblNews();
            obj.Id = Guid.NewGuid();
            obj.TextId = TblNewsRepository.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text, 1000);
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
            obj.DisplayOrder = 100;
            obj.ImageUrl = "";
        }
        obj.CategoryId = new Guid(ddlCategory.SelectedValue);
        obj.Title = UnicodeConversion.CreateInstant().CleanUpWordHtml(txtName.Text);
        obj.Summary = UnicodeConversion.CreateInstant().CleanUpWordHtml(txtSummary.Text);
        obj.Description = radEditorContent.Content;
        //string strSaveImage = SaveImage(FileUpload1.PostedFile, obj.TextId);
        //if (!strSaveImage.Equals(string.Empty))
        obj.ImageUrl = hdfimg.Value;
        obj.IsHot = cbHot.Checked;
        obj.IsVisible = cbIsVisible.Checked;
        obj.IsPublish = false;

        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (NewsId.Equals(Guid.Empty))
            TblNewsRepository.CreateInstant().Insert(GetTblNews());
        else
            TblNewsRepository.CreateInstant().Update(GetTblNews());
        Response.Redirect("NewsList.aspx");
    }

    //string SaveImage(HttpPostedFile file, string TextId)
    //{
    //    string result = string.Empty;
    //    try
    //    {
    //        if (!file.FileName.Equals(string.Empty))
    //        {
    //            string FolderName = "userfiles/news";
    //            string FileName = file.FileName;
    //            string physicalFolderFullPath = HttpContext.Current.Server.MapPath("/" + FolderName);
    //            string _ext = "." + file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
    //            if (!System.IO.Directory.Exists(physicalFolderFullPath)) System.IO.Directory.CreateDirectory(physicalFolderFullPath); // create image folder if not existed
    //            try
    //            {
    //                System.IO.File.Delete(physicalFolderFullPath + "/" + TextId + _ext);
    //            }
    //            catch { }

    //            file.SaveAs(physicalFolderFullPath + "/" + TextId + _ext);
    //            result = "/" + FolderName + "/" + TextId + _ext;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw new Exception("Error Upload Image " + ex);
    //    }
    //    return result;
    //}

}
